#include <QtGui>
#include <QXmlStreamWriter>
#include <QFile>
#include <iostream>
#include <QSqlDatabase>
#include <QSqlQuery>
#include "include/define.h"

#include "./include/exportDlg.h"

ExportDlg::ExportDlg(QWidget *parent)
    : QDialog(parent)
{
    setWindowFlags(Qt::FramelessWindowHint);
    setFixedSize(360, 160);

    CreateWidget();

}

void ExportDlg::CreateWidget()
{
    hintLabel = new QLabel(QString::fromUtf8("请选择导出位置"), this);
    QFont tmpFont = hintLabel->font();
    tmpFont.setPointSize(18);
    tmpFont.setBold(true);
    hintLabel->setFont(tmpFont);

    filePathEdit = new QLineEdit(this);
    exportBtn = new QPushButton(QString::fromUtf8("导出"), this);
    cancleBtn = new QPushButton(QString::fromUtf8("取消"), this);
    openFileBtn = new QPushButton(QString::fromUtf8("浏览"), this);

    exportBtn->setEnabled(false);

    connect(exportBtn, SIGNAL(clicked()), this, SLOT(SaveToFile()));
    connect(cancleBtn, SIGNAL(clicked()), this, SLOT(close()));
    connect(openFileBtn, SIGNAL(clicked()), this, SLOT(SearchFile()));
}

void ExportDlg::paintEvent(QPaintEvent *)
{
    QPainter painter(this);
    painter.drawLine(0, 51, 360, 51);

    QPainter painterRect(this);
    painterRect.setPen(Qt::white);
    painterRect.setBrush(Qt::white);
    painterRect.drawRect(0, 0, 360, 50);

    hintLabel->setGeometry(84, 10, 300, 30);
    filePathEdit->setGeometry(20, 80, 250, 24);
    openFileBtn->setGeometry(280, 80, 60, 24);
    exportBtn->setGeometry(220, 120, 60, 24);
    cancleBtn->setGeometry(290, 120, 60, 24);
}

void ExportDlg::SearchFile()
{
    QString filePath = QFileDialog::getSaveFileName(
                        this,
                        QString::fromUtf8("保存文件"),
                        QDir::currentPath(),
                        "Document files (*.xml)"
                       );
    if (!filePath.isNull())
    {
        filePathStr = filePath;
        filePathEdit->setText(filePath);
        exportBtn->setEnabled(true);
        qDebug() << filePathStr;
    }
}

void ExportDlg::SaveToFile()
{
    WriteXml(filePathStr);
    this->close();
}

bool ExportDlg::WriteXml(const QString &fileName)
{
    QFile file(fileName);
    if(!file.open(QFile::WriteOnly | QFile::Text))
    {
        std::cerr << "Error: Cannot write file "
                  << qPrintable(fileName) << ": "
                  << qPrintable(file.errorString()) << std::endl;
        return false;
    }

    QXmlStreamWriter xmlWriter(&file);
    xmlWriter.setDevice(&file);
    xmlWriter.setAutoFormatting(true);
    xmlWriter.writeStartDocument();
    xmlWriter.writeStartElement("itemInfo");
    for (int i=0; i<NUM_OF_TOTAL_ITEM; i++)
    {
        WriteItem(i, &xmlWriter);
    }
    xmlWriter.writeEndElement();
    xmlWriter.writeEndDocument();

    file.close();
    if (file.error())
    {
        std::cerr << "Error: Cannot write file "
                  << qPrintable(fileName) << ": "
                  << qPrintable(file.errorString()) << std::endl;
        return false;
    }

    return true;
}

void ExportDlg::WriteItem(int index, QXmlStreamWriter *xmlWriter)
{
    QString area;
    QString ridgepole;
    QString layer;
    QString room;
    QString devName;

    xmlWriter->writeStartElement("item");
    xmlWriter->writeAttribute("id", QString::number(index));
    QSqlDatabase::database().transaction();
    QSqlQuery query;

    query.exec(QString("select id, area, ridgepole, layer, room, devName from itemInfo where id = '%1'").arg(index));

    while(query.next())
    {
        area = query.value(1).toString();
        ridgepole = query.value(2).toString();
        layer = query.value(3).toString();
        room = query.value(4).toString();
        devName = query.value(5).toString();
    }

    xmlWriter->writeTextElement("area", area);
    xmlWriter->writeTextElement("ridgepole", ridgepole);
    xmlWriter->writeTextElement("layer", layer);
    xmlWriter->writeTextElement("room", room);
    xmlWriter->writeTextElement("devName", devName);

    xmlWriter->writeEndElement();
    QSqlDatabase::database().commit();
}
